﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLBH.Category.Presenter;
using PluginFramework.Common;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using QLBH.Model.Controller;
using QLBH.FeatureBase.Modules.Category;
using QLBH.FeatureBase;
using QLBH.Base;
using QLBH.ModelInterface;

namespace QLBH.Category
{
    [ExtensionOf(typeof(QLNhomHangToolExtensionPoint))]
    public class QLNhomHangTool : BaseView,IQLNhomHangTool
    {
        public QLNhomHangTool()
            :base()
        {

        }
        public FormViewBase GetView(object arg)
        {
            return new QLNhomHangTool();
        }
        public override void AddNew(object sender, EventArgs e)
        {
            AddNhomHangDlg addnew = new AddNhomHangDlg();
            if (addnew.ShowDialog()==DialogResult.OK)
            {
                LoadDSNhomHang();
            }
            base.AddNew(sender, e);
        }
        public override void EditItem(object sender, EventArgs e)
        {
            var model=DataGridView.SelectedRows[0].DataBoundItem as IQLNhomHangModel;
            AddNhomHangDlg editdlg = new AddNhomHangDlg();
            editdlg.DlgType = eDlgType.EDIT;
            editdlg.Model = model;
            if (editdlg.ShowDialog() == DialogResult.OK)
            {
                LoadDSNhomHang();
            }
            base.EditItem(sender, e);
        }
        public override void DeleteItem(object sender, EventArgs e)
        {
            var model = DataGridView.SelectedRows[0].DataBoundItem as IQLNhomHangModel;
            NhomHangController.GetInstance().Delete(model.ToEntity());
            LoadDSNhomHang();
            base.DeleteItem(sender, e);
        }
        public override void BuildSearchMenu()
        {
            ButtonItem btnSearchByID = new ButtonItem("btnSearchByID", "Tìm theo Mã nhóm hàng");
            ButtonItem btnSearchByName = new ButtonItem("btnSearchByName", "Tìm theo Tên nhóm hàng");
            ButtonItem btnSearchByTenNganhHang = new ButtonItem("btnSearchByName", "Tìm theo Tên ngành hàng");

            BtnSearch.SubItems.Add(btnSearchByID);
            BtnSearch.SubItems.Add(btnSearchByName);
            BtnSearch.SubItems.Add(btnSearchByTenNganhHang);

            btnSearchByID.Click += btnSearchByID_Click;

            btnSearchByName.Click += btnSearchByName_Click;

            btnSearchByTenNganhHang.Click += btnSearchByTenNganhHang_Click;
            base.BuildSearchMenu();            
        }

        void btnSearchByTenNganhHang_Click(object sender, EventArgs e)
        {
            List<IQLNhomHangModel> source = (List<IQLNhomHangModel>)DataSource;

            source = source.Where(m => m.TenNH.Contains(TbxSearchInput.ControlText)).ToList();

            DataGridView.DataSource = source;
        }

        void btnSearchByName_Click(object sender, EventArgs e)
        {
            List<IQLNhomHangModel> source = (List<IQLNhomHangModel>)DataSource;

            source = source.Where(m => m.TenLH.Contains(TbxSearchInput.ControlText)).ToList();

            DataGridView.DataSource = source;
        }

        void btnSearchByID_Click(object sender, EventArgs e)
        {
            List<IQLNhomHangModel> source = (List<IQLNhomHangModel>)DataSource;

            source = source.Where(m => m.MaLH.Contains(TbxSearchInput.ControlText)).ToList();

            DataGridView.DataSource = source;
        }
        public override void RefreshData(object sender, EventArgs e)
        {
            LoadDSNhomHang();
            base.RefreshData(sender, e);
        }
        private void LoadDSNhomHang()
        {
            QLNhomHangPresenter presenter = new QLNhomHangPresenter();
            DataSource = presenter.LoadDSNhomHang();
            DataGridView.DataSource = DataSource;

            DataGridView.Columns["ID"].Visible = false;
            DataGridView.Columns["ID"].DisplayIndex = 7;

            DataGridView.Columns["MaLH"].HeaderText = "Mã LH";
            DataGridView.Columns["MaLH"].DisplayIndex = 0;

            DataGridView.Columns["TenLH"].HeaderText = "Tên LH";
            DataGridView.Columns["TenLH"].DisplayIndex = 1;

            DataGridView.Columns["Mota"].HeaderText = "Mô Tả";
            DataGridView.Columns["Mota"].DisplayIndex = 2;

            DataGridView.Columns["GhiChu"].HeaderText = "Ghi Chú";
            DataGridView.Columns["GhiChu"].DisplayIndex = 3;

            DataGridView.Columns["NgayTao"].HeaderText = "Ngày Tạo";
            DataGridView.Columns["NgayTao"].DisplayIndex = 4;

            DataGridView.Columns["TenNH"].HeaderText = "Ngành Hàng";
            DataGridView.Columns["TenNH"].DisplayIndex = 5;

            DataGridView.Columns["NganhHangID"].Visible = false;
            DataGridView.Columns["NganhHangID"].DisplayIndex = 6;
        }
        public override void LoadForm(object sender, EventArgs e)
        {
            base.LoadForm(sender, e);
            LoadDSNhomHang();
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // QLNhomHangTool
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
            this.Name = "QLNhomHangTool";
            this.Size = new System.Drawing.Size(0, 0);
            this.ResumeLayout(false);

        }

        
    }
}
